﻿#include "lvgl.h"
#include "mainPage.h"
#include "page.h"
#include "led.h"
#include "mp3Page.h"
#include "weather.h"
#include "lvgl.h"
#include "mainPage.h"
#include "page.h"
#include "led.h"
#include "weather.h"
#include "calendar.h"
#include "smart_config.h"
static lv_style_t mainPageStyle;
static lv_style_t mainPageStyleBg;
struct main_page_item mp3Item;
struct main_page_item ledItem;
struct main_page_item weatherItem;
struct main_page_item calendarItem;
struct main_page_item wifiItem;

void main_page_item_create(main_page_item_t item, lv_obj_t* p, char* str, int width, int hight, lv_color_t bgcolor)
{
    item->btn = lv_btn_create(p);
    item->lable = lv_label_create(item->btn);
    lv_obj_set_size(item->btn, width, hight);

    lv_style_init(&item->style);
    lv_style_set_pad_all(&item->style, 10);
    lv_style_set_radius(&item->style, 0);//圆角

    lv_style_set_bg_opa(&item->style, LV_OPA_COVER);//背景透明度
    lv_style_set_bg_color(&item->style, bgcolor);//背景上面颜色

    lv_obj_add_style(item->btn, &item->style, 0);
    lv_label_set_text(item->lable, str);
    lv_obj_center(item->lable);
    
}

void main_page_show()
{
    lv_group_remove_all_objs(group);
    lv_group_add_obj(group, mp3Item.btn);
    lv_group_add_obj(group, ledItem.btn);
    lv_group_add_obj(group, weatherItem.btn);
    lv_group_add_obj(group, calendarItem.btn);
    lv_group_add_obj(group, wifiItem.btn);
    lv_scr_load(mainPage.page);
    push_page(&mainPage);
}

void mp3_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t* obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        if (key == LV_KEY_ENTER)
        {
            show_mp3_page();
        }
        
    }
}
void weather_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t* obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        if (key == LV_KEY_ENTER)
        {
            show_weather_page();
        }
    }
}
void wifi_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t* obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        if (key == LV_KEY_ENTER)
        {
            show_smart_config_wait();
        }
    }
}
void calendar_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t* obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        if (key == LV_KEY_ENTER)
        {
            show_calendar_page();
        }
    }
}
void led_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_KEY)
    {
        lv_obj_t* obj = lv_event_get_target(e);
        uint32_t key = lv_indev_get_key(lv_indev_get_act());
        if (key == LV_KEY_ENTER)
        {
            show_led_page();
        }
    }
}

void main_page_init()
{
    if (mainPage.page == NULL)
    {
        mainPage.page = lv_obj_create(0);
        lv_style_init(&mainPageStyle);
        lv_style_init(&mainPageStyleBg);

        /*渐变色*/
        lv_style_set_bg_opa(&mainPageStyleBg, LV_OPA_COVER);//背景透明度
        //lv_style_set_bg_color(&lvgl_ZhuJieMian_main_cont_style, LV_STATE_DEFAULT, lv_color_hex(0x1b1d5c));//背景上面颜色
        //lv_style_set_bg_grad_color(&lvgl_ZhuJieMian_main_cont_style, LV_STATE_DEFAULT, lv_color_hex(0x5c418d));//背景上面颜色
        lv_style_set_bg_color(&mainPageStyleBg, lv_color_hex(0));//背景上面颜色
        lv_style_set_bg_grad_color(&mainPageStyleBg, lv_color_hex(0));//背景上面颜色
        lv_style_set_bg_grad_dir(&mainPageStyleBg, LV_GRAD_DIR_VER);//渐变方向
        main_page_item_create(&mp3Item, mainPage.page, "mp3", 195, 40, lv_color_hex(0xee3f4d));
        main_page_item_create(&ledItem, mainPage.page, "led", 110, 40, lv_color_hex(0x322f3b));
        main_page_item_create(&weatherItem, mainPage.page, "weather", 110, 40, lv_color_hex(0x497568));
        main_page_item_create(&calendarItem, mainPage.page, "calendar", 195, 40, lv_color_hex(0xeed045));
        main_page_item_create(&wifiItem, mainPage.page, "wifi", 165, 40, lv_color_hex(0x22a2c3));
        lv_obj_set_pos(mp3Item.btn, 5, 5);
        lv_obj_align_to(ledItem.btn, mp3Item.btn, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
        lv_obj_add_event_cb(mp3Item.btn, mp3_event_handler, LV_EVENT_KEY, NULL);
        lv_obj_add_event_cb(ledItem.btn, led_event_handler, LV_EVENT_KEY, NULL);

        lv_obj_align_to(weatherItem.btn, mp3Item.btn,LV_ALIGN_OUT_BOTTOM_LEFT ,0, 5);
        lv_obj_align_to(calendarItem.btn, weatherItem.btn, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
        lv_obj_add_event_cb(weatherItem.btn, weather_event_handler, LV_EVENT_KEY, NULL);
        lv_obj_add_event_cb(calendarItem.btn, calendar_event_handler, LV_EVENT_KEY, NULL);

        lv_obj_align_to(wifiItem.btn, weatherItem.btn, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
        lv_obj_add_event_cb(wifiItem.btn, wifi_event_handler, LV_EVENT_KEY, NULL);
        mainPage.show = main_page_show;
    }
    main_page_show();
}
